package com.atguigu01.use.exer1;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * ClassName: Employee
 * Package: com.atguigu01.use.exer1
 * Description:
 *
 * @Author BuTang
 * @Create 2023/6/10 10:27
 * @Version 1.0
 */
public class Employee implements Comparable {




    private String name;
    private int age;
    private MyDate birthday;

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday
                ;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Employee){
            Employee e = (Employee) o;
          return this.getName().compareTo(e.getName());
        }
        throw new RuntimeException("比较类型不正确");
    }
}
class test1{
    public static void main(String[] args) {
        Employee e1 = new Employee("Tom",52,new MyDate(1992,12,22));
        Employee e2 = new Employee("Jerry",22,new MyDate(1988,12,22));
        Employee e3 = new Employee("Abe",2,new MyDate(1923,12,22));
        Employee e4 = new Employee("Lisa",23,new MyDate(1992,12,22));
        Employee e5 = new Employee("Angel",12,new MyDate(1956,12,22));

        Comparator comparator = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee && o2 instanceof Employee){
                    Employee e1 = (Employee) o1;
                    Employee e2 = (Employee) o2;
                    int yearcompare = e1.getBirthday().getYear() - e2.getBirthday().getYear();
                    if (yearcompare == 0) return 0;
                    else  {
                        return yearcompare;

                    }
                }
                throw new RuntimeException("你的brithday似乎输入错误");
            }
        };
        TreeSet<Employee> set = new TreeSet(comparator);

        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);



        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    }
